package com.project.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.project.entity.Admin;
import com.project.entity.BaseResult;
import com.project.entity.Room;
import com.project.mapper.RoomMapper;
import com.project.service.AdminService;
import com.project.service.RoomService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/room")
public class RoomController {
    @Autowired
    private RoomService roomService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private RoomMapper roomMapper;
    @PostMapping("/show")
    public BaseResult<List<Room>> show(){
        List<Room> rooms=roomMapper.selectList(null);;
        return BaseResult.ok(rooms);
    }
    @PostMapping("/edit")
    public BaseResult<String> edit(HttpServletRequest request,@RequestBody Room room){
        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        Long id = (Long) request.getSession().getAttribute("admin");
        queryWrapper.eq(Admin::getId, id);
        Admin adm = adminService.getOne(queryWrapper);
        if(adm.getAdminStatus()==1&&adm.getAdminKind()=="超级管理员"){
            roomService.updateById(room);
            return BaseResult.ok("客房更新成功");
        }
        return BaseResult.fail("客房更新失败");


    }
    @PostMapping("/new")
     public BaseResult<String> add(HttpServletRequest request,@RequestBody Room room){
        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        Long id = (Long) request.getSession().getAttribute("admin");
        queryWrapper.eq(Admin::getId, id);
        Admin adm = adminService.getOne(queryWrapper);
        if(adm.getAdminStatus()==1&&adm.getAdminKind()=="超级管理员"){
            log.info("客房信息：{}",room.toString());
            roomService.save(room);
            return BaseResult.ok("客房录入成功");
        }
        return BaseResult.fail("客房录入失败");

    }
    @PostMapping("/drop")
    public BaseResult<String> drop(HttpServletRequest request,@RequestBody Room room){
        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        Long id = (Long) request.getSession().getAttribute("admin");
        queryWrapper.eq(Admin::getId, id);
        Admin adm = adminService.getOne(queryWrapper);
        if(adm.getAdminStatus()==1&&adm.getAdminKind()=="超级管理员"){
            LambdaQueryWrapper<Room> queryWrapper1 = new LambdaQueryWrapper<>();
            Long id1 = room.getId();
            queryWrapper1.eq(Room::getId, id1);
            roomService.remove(queryWrapper1);
            return BaseResult.ok("删除成功");
        }
        return BaseResult.fail("删除失败");

    }

}
